$("#form-user-add").validate({
	rules:{
		userName:{
			required:true,
			minlength: 2,
			maxlength: 20,
			remote: {
                url: ctx + "system/user/checkUserNameUnique",
                type: "post",
                dataType: "json",
                data: {
                	name : function() {
                        return $.trim($("#loginName").val());
                    }
                },
                dataFilter: function(data, type) {
                    if (data == "0") return true;
                    else return false;
                }
            }
		},
		nickName:{
			required:true,
		},
		password:{
			required:true,
			minlength: 5,
			maxlength: 20
		},
		email:{
			required:true,
            email:true,
            remote: {
                url: ctx + "system/user/checkEmailUnique",
                type: "post",
                dataType: "json",
                data: {
                    name: function () {
                        return $.trim($("#email").val());
                    }
                },
                dataFilter: function (data, type) {
                    if (data == "0") return true;
                    else return false;
                }
            }
		},
		telephone:{
			required:true,
			isPhone:true,
            remote: {
                url: ctx + "system/user/checkPhoneUnique",
                type: "post",
                dataType: "json",
                data: {
                    name: function () {
                        return $.trim($("#telephone").val());
                    }
                },
                dataFilter: function (data, type) {
                    if (data == "0") return true;
                    else return false;
                }
            }
		},
	},
	messages: {
        "userName": {
            remote: "用户已经存在"
        },
		"email": {
            remote: "Email已经存在"
        },
		"telephone":{
        	remote: "手机号码已经存在"
		}
    },
	submitHandler:function(form){
		add();
	}
});

function add() {
	var id = $("input[name='id']").val();
	var userName = $("input[name='userName']").val();
    var nickName = $("input[name='nickName']").val();
	var password = $("input[name='password']").val();
	var email = $("input[name='email']").val();
	var telephone = $("input[name='telephone']").val();
	var sex = $("input[name='sex']:checked").val();
	var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
	var roleIds = $.getCheckeds("role");
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx + "system/user/save",
		data : {
			"id": id,
            "userName": userName,
			"nickName": nickName,
			"password": password,
			"email": email,
			"telephone": telephone,
			"sex": sex,
			"status": status,
			"roleIds": roleIds
		},
		async : false,
		error : function(request) {
			$.modalAlert("系统错误", modal_status.FAIL);
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("新增成功,正在刷新数据请稍后……",{icon:1,time: 500,shade: [0.1,'#fff']},function(){
					$.parentReload();
				});
			} else {
				$.modalAlert(data.msg, modal_status.FAIL);
			}

		}
	});
}
